﻿using System;
using System.Collections.Generic;
 
using System.Text;
using System.Net;
using System.IO;
using GGSharp.Http;
using System.Threading;

namespace GGSharp
{
    public class GGHttp
    {
        protected const string USER_AGENT = "Mozilla/5.0 (Windows; U; Windows NT 6.0; pl; rv:1.9.0.4) Gecko/2008102920 Firefox/3.0.4 (.NET CLR 3.5.30729)";

        protected const string HOST_REGISTER = "register.gadu-gadu.pl";
        protected const string HOST_RETRIEVE = "retr.gadu-gadu.pl";
        protected const string ADDRESS_GET_TOKEN = "/appsvc/regtoken.asp";
        protected const string ADDRESS_REGISTER = "/appsvc/fmregister3.asp";
        protected const string ADDRESS_RETRIEVE = "/appsvc/fmregister3.asp";

        protected static void ReceiveResponse(string url, HttpStructureDataBase data, HttpStructureBase responseObject)
        {
            WorkObject work = new WorkObject();
            work.Address = url;
            work.Response = responseObject;
            work.Request = data;

            ThreadPool.QueueUserWorkItem(new WaitCallback(ReceiveResponse_WaitCallback), work);
        }

        protected static void ReceiveResponse_WaitCallback(object data)
        {
            WorkObject work = (WorkObject)data;

            try
            {
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(work.Address);
                request.Method = "POST";
                request.UserAgent = USER_AGENT;
                request.ContentLength = work.Request != null ? work.Request.QueryString.Length : 0;
                request.ContentType = "application/x-www-form-urlencoded";

                work.Response.State = RequestState.Connecting;

                if(work.Request != null)
                {
                    StreamWriter writer = new StreamWriter(request.GetRequestStream(), Encoding.GetEncoding("windows-1250"));
                    writer.Write(work.Request.QueryString);
                }

                StreamReader reader = new StreamReader(request.GetResponse().GetResponseStream(), Encoding.GetEncoding("windows-1250"));
                work.Response.Read(reader);

                reader.Close();
            }
            catch (Exception e)
            {
                work.Response.State = RequestState.Error;
            }
        }

        public static AccountOperationInfo ChangePassword(ChangePasswordData data)
        {
            AccountOperationInfo accountOperation = new AccountOperationInfo();
            ReceiveResponse("http://" + HOST_REGISTER + ADDRESS_REGISTER, data, accountOperation);
            return accountOperation;
        }

        public static AccountOperationInfo DeleteAccount(DeleteAccountData data)
        {
            AccountOperationInfo accountOperation = new AccountOperationInfo();
            ReceiveResponse("http://" + HOST_REGISTER + ADDRESS_REGISTER, data, accountOperation);
            return accountOperation;
        }

        public static TokenInfo GetToken()
        {
            TokenInfo token = new TokenInfo();
            ReceiveResponse("http://" + HOST_REGISTER + ADDRESS_GET_TOKEN, null, token);
            return token;
        }

        public static RetrievePasswordInfo RetrievePassword(RetrievePasswordData data)
        {
            RetrievePasswordInfo retPasswordInfo = new RetrievePasswordInfo();
            ReceiveResponse("http://" + HOST_RETRIEVE + ADDRESS_RETRIEVE, data, retPasswordInfo);
            return retPasswordInfo;
        }

        public static RegisterAccountInfo RegisterAccount(RegisterAccountData data)
        {
            RegisterAccountInfo regAccountInfo = new RegisterAccountInfo();
            ReceiveResponse("http://" + HOST_REGISTER + ADDRESS_REGISTER, data, regAccountInfo);
            return regAccountInfo;
        }


    }
}
